package log_stash

import (
	"blog_go/global"
	"github.com/sirupsen/logrus"
)

var logSyncDBChan chan *LogStashModel = make(chan *LogStashModel, 500)

// 是否异步
var sync bool = true

func init() {
	if sync {
		InitLogStashSync()
	}
}

func InitLogStashSync() {
	go logStashSyncDBLoop()
}

func logStashSyncDBLoop() {
	for {
		select {
		case data := <-logSyncDBChan:
			err := global.DB.Create(data).Error
			if err != nil {
				logrus.Error("行为日志错误", *data, err)
			}
		}
	}
}
